<template>
  <div class="container">
    <img class="img1" src="../assets/images/background3.jpeg" alt="" />
    <div class="app">
      <div class="left">
        <div>
          <h3>签名请在下面的方格中</h3>
        </div>
        <sign-canvas
          class="sign-canvas"
          ref="SignCanvas"
          :options="options"
          v-model="value"
        />
        <img class="view-image" :src="value" width="150" height="150" />
        <div class="sign-btns">
          <span id="loadNew" @click="loadNew()">新加载</span>
          <span id="coverLoad" @click="coverLoad()">合并加载</span>
          <span id="clear" @click="canvasClear()">清空</span>
          <span id="save" @click="saveAsImg()">保存</span>
          <span id="downL" @click="downloadSignImg()">下载</span>
        </div>
      </div>
      <div class="right">
        <input class="name" v-model="name" placeholder="请输入姓名" />
        <input class="job" v-model="job" placeholder="请输入工作" />
        <!-- <textarea
          v-model="remark1"
          class="remark"
          cols="22"
          rows="10"
          placeholder="请输入留言(最多输入30个字)"
          maxlength="30"
        >
        </textarea> -->
      </div>
    </div>
  </div>
</template>

<script>
import { GetAddName } from "../api";
export default {
  name: "Writer",
  data() {
    return {
      value: null,
      options: {
        isSign: true, //签名模式 [Boolean] 默认为非签名模式,有线框, 当设置为true的时候没有任何线框
        isShowBorder: false, //是否显示边框 [可选]
      },
      name: "", //姓名
      // remark1: "",
      job: "", //工作
    };
  },
  methods: {
    /**
     * 新加载图像
     */
    loadNew() {
      this.$refs.SignCanvas.loadNew(
        "https://testfi.oss-cn-hangzhou.aliyuncs.com/1577178007000.png"
      );
    },

    /**
     * 合并加载
     */
    coverLoad() {
      this.$refs.SignCanvas.coverLoad(
        "https://testfi.oss-cn-hangzhou.aliyuncs.com/1577178007000.png"
      );
    },

    /**
     * 清除画板
     */
    canvasClear() {
      this.$refs.SignCanvas.canvasClear();
    },

    /**
     * 保存图片
     */
    saveAsImg() {
      const img = this.$refs.SignCanvas.saveAsImg();
      var img1 = img.toString();
      if (
        this.name !== undefined &&
        this.name !== "" &&
        this.job !== undefined &&
        this.job !== ""
      ) {
        GetAddName({ imgUrl: img1, userName: this.name, job: this.job }).then(
          (res) => {
            if (res.status == 200) {
              // console.log(res);
              this.$router.push("/showwriters");
            } else {
              this.$message.error(res.data.msg);
            }
          }
        );
      } else {
        this.$message.error("请输入姓名或者工作");
      }
    },

    /**
     * 下载图片
     */
    downloadSignImg() {
      this.$refs.SignCanvas.downloadSignImg();
    },
  },
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.container {
  background: #eb7072;
  width: 100%;
}
.img1 {
  position: absolute;
  width: 100%;
  height: 100%;
}
.app {
  width: 1200px;
  margin: 0 auto;
  margin-top: 30px;
  position: absolute;
  z-index: 99;
  top: 5%;
  left: 20%;
}
.left {
  width: 800px;
  float: left;
}
.left h3 {
  color: #eb7072;
}
.right {
  width: 400px;
  float: left;
}
.name {
  border: 1px dashed #f00;
  outline: none;
}
.job {
  border: 1px dashed #f00;
  outline: none;
}
.remark {
  display: block;
  margin-top: 50px;
  border: 1px dashed #f00;
  height: 525px;
  outline: none;
  resize: none;
}
* {
  margin: 0;
  padding: 0;
}
.sign-canvas {
  display: block;
  border: 1px dashed #f00;
}
.view-image {
  display: block;
  margin: 0 auto;
}
.sign-btns {
  width: 800px;
  display: flex;
  justify-content: space-between;
  margin-left: -70px;
  #loadNew,
  #coverLoad,
  #clear,
  #downL,
  #save {
    margin: 0 auto;
    display: inline-block;
    padding: 5px 10px;
    width: 150px;
    height: 40px;
    line-height: 40px;
    border: 1px solid #eee;
    background: #e1e1e1;
    border-radius: 10px;
    text-align: center;
    margin: 20px auto;
    cursor: pointer;
  }
}
</style>
